package com.anti.modular.sys.role.service.impl;

import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.role.dao.SysRoleMenuDao;
import com.anti.modular.sys.role.entity.SysRoleMenu;
import com.anti.modular.sys.role.param.SysRoleParam;
import com.anti.modular.sys.role.service.SysRoleMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-10
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Resource
    DBUtils dbUtils;

    @Resource
    SysRoleMenuDao sysRoleMenuDao;
    @Override
    public List<Long> getRoleMenuIdList(List<Long> roleIdList) {
        String sql="select DISTINCT id from sys_menu WHERE id in (select menu_id from sys_role_menu where role_id in (:roleId)) and status=0";
        Map param=new HashMap();
        param.put("roleId",roleIdList);
        List<Long> list = dbUtils.query(sql, Long.class, param);
        return list;
    }

    @Override
    public void saveGrantMenu(SysRoleParam sysRoleParam) {
        Long roleId = sysRoleParam.getId();
        //删除所拥有菜单
        sysRoleMenuDao.delete(roleId);
        //授权菜单
        sysRoleParam.getGrantMenuIdList().forEach(menuId -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuDao.save(sysRoleMenu);
        });
    }
}
